<template>
  <div style="width: 600px;margin: 20px auto">
    <el-form ref="changePwdForm" size="small" :model="entity" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="old password" prop="oldpwd">
        <el-input v-model="entity.oldpwd" show-password placeholder="old password" ref="oldpwd" />
      </el-form-item>
      <el-form-item label="new password" prop="newpwd">
        <el-input v-model="entity.newpwd" show-password placeholder="new password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit" @click="btnSubmit">ok</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  data() {
    return {
      entity: {
        oldpwd: '',
        newpwd : '',
      },
      rules: {
        oldpwd: [
          { required: true, message: 'please input old password', trigger: 'blur' },
          { min: 8, max: 20, message: 'Length between 8 and 20 characters', trigger: 'blur' }
        ],
        newpwd: [
          { required: true, message: 'please input new password', trigger: 'blur' },
          { min: 8, max: 20, message: 'Length between 8 and 20 characters', trigger: 'blur' }
        ],
      }
    }
  },
  mounted() {
    this.$nextTick(()=>{
      this.$refs.oldpwd.focus()
    })
  },
  methods: {
    btnSubmit(){
      this.$refs['changePwdForm'].validate((valid) => {
        if(valid){
          request({
            url: '/sys/useradmin/changepwd',
            data: this.entity
          }).then( res =>{
            if(res.code === "200"){
              this.$message.success(res.msg)
            }else{
              this.$message.error(res.msg);
            }
          })
        }else{
          return false;
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
